<?php

namespace app\mobile\controller;

use app\model\Theme;

class EditTheme extends Base
{
    public function index()
    {
        global $_G, $_P;
        $op = trim($_P['op']);
        $system = $_G['system'];
        $themeid = intval($_P['themeid']);
        $theme = Theme::getThemeById($themeid);
        if (empty($theme['id'])) {
            $result['error'] = 1;
            $result['msg'] = "活动不存在或编号错误；";
            return return_msg($result, '', 'ajax');
        }
        $config = Theme::getConfig($themeid, true);

        if (strstr(YIXUAN_IP, $_G['clientip']) == false) {
            $result['error'] = 1;
            $result['msg'] = "此 " . $_G['clientip'] . " IP地址未授权！";
            return return_msg($result, '', 'ajax');
        }
        if ($op == 'edit') {
//https://high.yixuanbk.cn/md61962/2/1/0/edit_theme.aspx?&soft=W0oCHpJKB&op=edit
            $postdata = array(
                'title' => $_P['title'],
                'desc' => $_P['desc'],
                'bannerimgs' => $_P['bannerimgs'],
                'centerimgs' => $_P['centerimgs'],
                'imgs' => $_P['imgs'],
                'content' => $_P['content'],//活动主图
                'addimg' => $_P['addimg'],//底部二维码
                'pinpailogo' => $_P['pinpailogo'],//主办方LOGO
                'pageshare_txt' => $_P['pageshare_txt'],//分享海报文字
            );
            if ($_P['ispresale'] == 1 && $system['ispresale'] == 1) {
                $config_old = $config;
                if (DOMAIN0 == "30.yixuan.net") {
                    $config['presale']['is_upload_psd'] = 1;
                } elseif ($config['presale']['isdesign'] == 0) {
                    return return_msg(array('error' => 1, 'msg' => '当前活动已关闭在线编辑'), '', 'ajax');
                }
                $config['presale']['headlogo'] = $_P['bannerimgs'][0];
                $config['presale']['bannerimgs'] = $_P['bannerimgs'];
                $config['presale']['centerimgs'] = $_P['centerimgs'];
                $config['presale']['imgs'] = $_P['imgs'];
                $config['presale']['content'] = Theme::format_content($_P['content']);
                $config['presale']['addimg'] = $_P['addimg'];
                $config['presale']['isdesign'] = 1;
                if ($system['wxapp'] != 0 && $system['softmode'] != 0) {
                    $config['presale']['contentxcx'] = $_P['content'];
                    $config['presale']['addimgxcx'] = $_P ['addimg'];
                }
                Theme::setConfig($config, $themeid, $_G['mpid']);
                writelog($themeid, '活动编辑补款', $config_old['presale'], $config['presale'], '设计平台');
                Theme::insert_attachment($_P);
            } else {
                $theme_old = $theme;
                $config_old = $config;
                if (DOMAIN0 != "30.yixuan.net" && $config['isdesign'] == 0) {
                    return return_msg(array('error' => 1, 'msg' => '当前活动已关闭在线编辑'), '', 'ajax');
                }
                $res = Theme::format_new_theme_cache($system, $theme, $config, $postdata);
                Theme::insert_attachment($_P);
                $theme = $res['theme'];
                $config = $res['config'];
                if (!empty($postdata['bannerimgs'])) {
                    db_update(t_theme, $theme, array('id' => $themeid));
                    writelog($themeid, '活动编辑theme', $theme_old, $theme, '设计平台');
                }
                Theme::setConfig($config, $themeid, $_G['mpid']);
                writelog($themeid, '活动编辑', $config_old, $config, '设计平台');
            }


            return return_msg($postdata, '', 'ajax');
        }

    }
}